/*
 * GameBoard.java
 *
 * Created on November 22, 2007, 6:50 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package hextd.UI;

import hextd.HexTD;
import hextd.util.Context;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 *
 * @author  hylke
 */
public class GameBoard extends javax.swing.JPanel {
    
    private HexTD game;
    private Context context;
    private float offsetX, offsetY;
    
    /**
     * Creates new form GameBoard
     */
    public GameBoard(HexTD game, Context context) {
        this.game = game;
        this.context = context;
        initComponents();
    }
    
    public void paint(Graphics g) {
//        try {
            this.game.paintBoard((Graphics2D) g);
//        } catch (IllegalArgumentException e) {
//            this.game.recalculateScale();
//            this.game.repaint();
//        } catch (NullPointerException e) {
//            this.game.recalculateScale();
//            this.game.repaint();
//        }
    }
    
    public int[] hexCoordsFromScreenCoords(int screenx, int screeny) {
        int row, column, hexX, hexY;
        float x, y, dx, dy, h, r, width, height;
        int[] hexC = new int[2];
        width = this.context.hexPaintWidth * 2;
        height = this.context.paintScale * 1.5f;
        h = this.context.paintScale / 2;
        r = this.context.hexPaintWidth;
        x = screenx - this.context.offsetX;
        y = screeny - this.context.offsetY;
        column = Math.round( -0.5f + x / width );
        row = Math.round( -0.5f + y / height );
        dx = x - column * width;
        dy = y - row * height;
        
        hexX = column;
        hexY = row;
        if ((row & 1) == 0) {
            if (dy < (h - (h/r) * dx)) {
                hexX--;
                hexY--;
            }
            if (dy < (-h + (h/r) * dx)) {
                hexY--;
            }
        } else {
            if (dx > r) {
                if (dy < h*2 - h/r*dx ) {
                    hexY--;
                }
            } else {
                if (dy < h/r*dx) {
                    hexY--;
                } else {
                    hexX--;
                }
            }
        }
        hexC[0] = hexX;
        hexC[1] = hexY;
        return hexC;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
        this.game.keyTyped(evt.getKeyChar());
    }//GEN-LAST:event_formKeyTyped
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
